package deck ;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.geom.Point;
import openfl.geom.Rectangle;


/**
 * ...
 * @author Marcos Martinez
 * cards w: 79
 * cards h: 123
 */

class Deck 
{
	public static var DIAMONDS:Int = 0;
	public static var HEARTS:Int = 1;
	public static var CLUBS:Int = 2;
	public static var SPADES:Int = 3;
	
	private var m_bmRawCards:Bitmap;
	public var m_bmRear:Bitmap;
	
	public var m_aCards:Array<Array<Card>>;
	public var m_aClubs:Array<Card>;
	public var m_aDiamonds:Array<Card>;
	public var m_aHearts:Array<Card>;
	public var m_aSpades:Array<Card>;
	
	private var m_nWidth:Float = 79;
	private var m_nHeight:Float = 123;

	public function new(bmRawCards:Bitmap) 
	{
		m_bmRawCards = bmRawCards;
		m_bmRear = extractCard(2 * m_nWidth, 4 * m_nHeight);
		m_aClubs= new Array<Card>();
		m_aDiamonds = new Array<Card>();
		m_aHearts = new Array<Card>();
		m_aSpades = new Array<Card>();
	}
	
	public function createCards():Void
	{
		 
		for (value in 0...13)
		{
			var posX:Float = value * m_nWidth;
			var posY:Float = 0;
			var bmCard:Bitmap = extractCard(posX, posY);
			var cCard:Card = new Card(bmCard, new Bitmap( m_bmRear.bitmapData));
			m_aClubs.push(cCard);
			
		}
				
		for (value in 0...13)
		{
			var posX:Float = value * m_nWidth;
			var posY:Float = 1*m_nHeight;
			var bmCard:Bitmap = extractCard(posX, posY);
			var cCard:Card = new Card(bmCard,  new Bitmap( m_bmRear.bitmapData));
			m_aDiamonds.push(cCard);
			
		}
		
		for (value in 0...13)
		{
			var posX:Float = value * m_nWidth;
			var posY:Float = 2*m_nHeight;
			var bmCard:Bitmap = extractCard(posX, posY);
			var cCard:Card = new Card(bmCard,  new Bitmap( m_bmRear.bitmapData));
			m_aHearts.push(cCard);
			
		}
		
		for (value in 0...13)
		{
			var posX:Float = value * m_nWidth;
			var posY:Float = 3 * m_nHeight;
			var bmCard:Bitmap = extractCard(posX, posY);
			var cCard:Card = new Card(bmCard,  new Bitmap( m_bmRear.bitmapData));
			m_aSpades.push(cCard);			
		}
			
		m_aCards = new Array<Array<Card>>();
		m_aCards.push(m_aDiamonds);	
		m_aCards.push(m_aHearts);	 
		m_aCards.push(m_aClubs);
		m_aCards.push(m_aSpades);		
	}
	
	private function extractCard(posX:Float , posY:Float):Bitmap
	{
		var bmDat:BitmapData = new BitmapData(Math.ceil(m_nWidth), Math.ceil(m_nHeight));
		bmDat.copyPixels(m_bmRawCards.bitmapData, new Rectangle(posX, posY, m_nWidth, m_nHeight), new Point(0, 0));
		return new Bitmap(bmDat);
	}
}